/**
 * Calendar class following specifications for iCal calendaring
 * objects as specified by RFC5545. The Calendar objects "exported"
 * by this class can be used with any calendaring application that
 * accepts iCal-type objects (.ics files).
 * 
 * @author nisoda, shultzam
 */
package application;

import java.util.ArrayList;
import java.io.File;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.PrintWriter;
import java.io.IOException;

/**
 * @author nisoda, shultzam
 */

public class Calendar {
	private ArrayList <CalEvent> savedEvents;
	private String calName;
	private final String version;
	private String tzid;
	private String tzName;
	private int tzOffset;
	
	/**
	 * Constructor for Calendar object.
	 */
	public Calendar() {
		this.savedEvents = new ArrayList <CalEvent> ();
		this.version = new String("2.0");
		this.calName = new String();
		this.tzid = new String();
	}
	
	/**
	 * Adds a CalEvent to ArrayList of CalEvent objects.
	 * 
	 * @param event
	 */
	public void addEvent(CalEvent event) {
		savedEvents.add(event);
	}
	
	/**
	 * Returns the number of CalEvents saved in this Calendar object.
	 * 
	 * @return Number of CalEvent objects
	 */
	public int numEvents() {
		return this.savedEvents.size();
	}
	
	/**
	 * Sets the name of the Calendar object (becomes name of
	 * exported file + ".ics").
	 * 
	 * @param name
	 */
	public void setCalName(String name) {
		this.calName = name;
	}
	
	/**
	 * Gets the CalEvent object at the specified index in the
	 * ArrayList savedEvents.
	 * 
	 * @param i
	 * @return CalEvent object at index i
	 */
	public CalEvent getEvent(int i) {
		return this.savedEvents.get(i);
	}
	
	/**
	 * 
	 * @param tzName
	 */
	public void setTZInfo(String tzName) {
		this.tzName = tzName;
		
		switch(this.tzName) {
			case "IDLW" : 	this.tzid = "International Date Line West";
						  	this.tzOffset = -1200; break;
			case "NT" : 	this.tzid = "Nome Time";
							this.tzOffset = -1100; break;
			case "HST" : 	this.tzid = "Hawaii Standard Time";
						 	this.tzOffset = -1000; break;
			case "AKST" : 	this.tzid = "Alaska Standard Time";
							this.tzOffset = -900; break;
			case "PST" : 	this.tzid = "Pacific Standard Time";
						 	this.tzOffset = -800; break;
			case "MST" : 	this.tzid = "Mountain Standard Time";
							this.tzOffset = -700; break;
			case "CST" : 	this.tzid = "Central Standard Time";
							this.tzOffset = -600; break;
			case "EST" : 	this.tzid = "Eastern Standard Time";
							this.tzOffset = -500; break;
			case "AST" :	this.tzid = "Atlantic Standard Time";
							this.tzOffset = -400; break;
			case "AT" : 	this.tzid = "Azores Time";
			 				this.tzOffset = -200; break;
			case "WAT" : 	this.tzid = "West Africa Time";
						 	this.tzOffset = -100; break;
			case "GMT" : 	this.tzid = "Greenwich Mean Time";
							this.tzOffset = 0; break;
			case "CET" : 	this.tzid = "Central European Time";
							this.tzOffset = 100; break;
			case "EET" : 	this.tzid = "Eastern European Time";
							this.tzOffset = 200; break;
			case "BT" : 	this.tzid = "Baghdad Time";
							this.tzOffset = 300; break;
			case "ZP4" : 	this.tzid = "USSR Zone 3";
							this.tzOffset = 400; break;
			case "ZP5"  :	this.tzid = "USSR Zone 4";
							this.tzOffset = 500; break;
			case "ZP6" : 	this.tzid = "USSR Zone 5";
							this.tzOffset = 600; break;
			case "CXT" : 	this.tzid = "Christmas Island Time";
							this.tzOffset = 700; break;
			case "CCT" :	this.tzid = "China Coast Time";
							this.tzOffset = 800; break;
			case "JST" : 	this.tzid = "Japan Standard Time";
							this.tzOffset = 900; break;
			case "EAST" :	this.tzid = "East Australian Standard Time";
							this.tzOffset = 1000; break;
			case "IDLE" : 	this.tzid = "International Date Line East";
							this.tzOffset = 1200; break;
			default : 		this.tzid = "Hawaii Standard Time";
						 	this.tzOffset = -1000; break;
		}
	}
	
	/**
	 * Writes all Calendar and CalEvent information to a .ics file
	 * as specified by RFC5545.
	 */
	public void exportCal() {
		        
		try {
            File outputICS = new File(this.calName + ".ics");
            
            FileWriter fileWriter = new FileWriter(outputICS.getAbsoluteFile());
            BufferedWriter buffWriter = new BufferedWriter(fileWriter);
            PrintWriter printer = new PrintWriter(buffWriter);
            
            printer.println("BEGIN:VCALENDAR");
            printer.println("VERSION:" + this.version);
            printer.println("BEGIN:VTIMEZONE");
            printer.println("TZID:" + this.tzid);
            printer.println("BEGIN:STANDARD");
            printer.println("TZOFFSETFROM:-1000");
            if(this.tzName.compareTo("GMT") == 0) {
            	printer.println("TZOFFSETTO:" + String.format("-%04d", this.tzOffset));
            }
            else {
            	printer.println("TZOFFSETTO:" + String.format("%05d", this.tzOffset));
            }
            printer.println("TZNAME:" + this.tzName);
            printer.println("END:STANDARD");
            printer.println("END:VTIMEZONE");
            
            for(CalEvent e : this.savedEvents) {
            	if(this.tzName.compareTo("GMT") != 0) {
                	e.adjustTZOffset(this.tzOffset);
                }
            	printer.print(e);
            }
              
            printer.println("END:VCALENDAR");
            
            printer.close();
		}
		catch(IOException e) {
            System.out.println("Error saving file.");
		}
	}
	
}
